<template>
  <div class="fullscreen">
    <div class="songMessage">
      <div class="songTitleName">{{this.music.name}}</div>
      <div class="songTitleSinger">{{this.music.song.artists[0].name}}</div>
    </div>
    <div @click="$emit('hidefull', false)" class="close">
      <van-icon name="cross" />
    </div>

    <div
      style="margin-top: 15px"
      class="cover"
      :style="{ animationPlayState: playstate ? 'running' : 'paused' }"
    >
      <img :src="music.picUrl || music.al.picUrl" />
    </div>
    <div class="lyric" style="margin-top: 15px">
      <ul :style="{ transform: `translateY(${toppx}px)` }">
        <template v-for="(l, i) in lyricArray">
          <li v-if="l" :key="i" :class="{ highlight: i == highIndex }">
            {{ l.lyric }}
          </li>
        </template>
      </ul>
    </div>
    <div class="controls">
      <div class="range">
        <span style="fontSize: 14px; color: #ccc">{{ currentTime | formatTime }}</span>
        <input style="height: 14px; margin: 0 5px" type="range" v-model="processTime" min="0" max="100" />
        <span style="fontSize: 14px; color: #ccc">{{ totalTime | formatTime }}</span>
      </div>
      <div class="ctrls" style="margin-top: 15px">
        <div @click="$emit('changemusic', 1)" class="previousSong">
          <img src="../assets/shangyishou.png" alt="" />
        </div>

        <div v-if="playstate" @click="play" class="playorpause" style="color: white">
          <van-icon name="pause-circle-o" />
        </div>

        <div v-else @click="play" class="playorpause" style="color: white">
          <van-icon name="play-circle-o" />
        </div>

        <div @click="$emit('changemusic', 2)" class="nextSong">
          <img src="../assets/xiayishou.png" alt="" />
        </div>

        <div class="latelySong" @click="changeLately">
          <img src="../assets/24gf-playlist2.png" alt="">
        </div>

        <div class="module">
          <div
            :class="{ active: cirlceMode == 'single' }"
            @click="$emit('changecircle', 'list')"
            class="single"
            v-if="this.cirlceMode === 'single'"
          >
            <div @click="singlePlay">
              <img src="../assets/danquxunhuan.png" alt="" />
            </div>
          </div>
          <div
            :class="{ active: cirlceMode == 'list' }"
            @click="$emit('changecircle', 'random')"
            class="list-play"
            v-if="this.cirlceMode === 'list'"
          >
            <div @click="listPlay">
              <img src="../assets/liebiaoxunhuan-2.png" alt="" />
            </div>
          </div>
          <div
            :class="{ active: cirlceMode == 'random' }"
            @click="$emit('changecircle', 'single')"
            class="random"
            v-if="this.cirlceMode === 'random'"
          >
            <div @click="randomPlay">
              <img src="../assets/suijibofang-2.png" alt="" />
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="lately" :style="{ bottom: lately ? '10px' : '-100%' }">
      <div class="title">最近播放</div>
      <div class="latelySongListBox">
        <ul
          class="latelySongList"
          v-for="(item, index) in playCollect"
          :key="index"
        >
          <li class="latelySongItem" @click="changeSong(item)">
            <div class="itemBox">
              <div
                class="latelySongItemName"
                :style="{
                  color:
                    music.name == item.name &&
                    music.song.artists[0].name == item.song.artists[0].name
                      ? '#e4393c'
                      : 'white',
                }"
              >
                {{ item.name }}
              </div>
              <div
                class="heng"
                :style="{
                  color:
                    music.name == item.name &&
                    music.song.artists[0].name == item.song.artists[0].name
                      ? '#e4393c'
                      : 'gray',
                }"
              >
                -
              </div>
              <div
                class="latelySongItemSinger"
                v-for="art in item.song.artists"
                :key="art.id"
                :style="{
                  color:
                    music.name == item.name &&
                    music.song.artists[0].name == item.song.artists[0].name
                      ? '#e4393c'
                      : 'gray',
                }"
              >
                {{ art.name }}/
              </div>
            </div>

            <music-loading
              :style="{
                opacity:
                  music.name == item.name &&
                  music.song.artists[0].name == item.song.artists[0].name
                    ? '1'
                    : '0',
              }"
            ></music-loading>
          </li>
        </ul>
      </div>
      <div class="closeLately" @click="changeLately">关闭</div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
import MusicLoading from "./MusicLoading.vue";
export default {
  components: { MusicLoading },
  props: {
    music: Object,
    playstate: {
      type: Boolean,
      default: true,
    },
    currentTime: {
      type: Number,
      default: 0,
    },
    totalTime: {
      type: Number,
      default: 0,
    },
    playCollect: Array,
    cirlceMode: {
      type: String,
      default: "list",
    },
  },
  data() {
    return {
      lyric: null,
      lately: false,
      loading: false,
    };
  },
  created() {
    this.getLyric();
  },
  methods: {
    async getLyric() {
      //获取歌词
      let re = await this.axios.get("/lyric?id=" + this.music.id);
      this.lyric = re.lrc.lyric;
    },
    changeLately() {
      this.lately = !this.lately;
    },
    changeSong(item) {
      this.$emit("changeSong", item);
    },

    play() {
      this.$emit("playorpaused");
    },
    singlePlay() {
      Toast("列表循环");
    },
    listPlay() {
      Toast("随机播放");
    },
    randomPlay() {
      Toast("单曲循环");
    },
  },
  computed: {
    toppx() {
      //计算属性 计算ul播放歌曲的时候移动的距离
      let t = this.highIndex * 30 - 100; // 歌词li元素的高度 * 当前歌词位置 -100居中
      return -t;
    },
    highIndex() {
      if (this.lyricArray.length < 2) return 0;
      //计算当前高亮的下标
      //判断当前播放大于等于最后一条歌词时间的时候 不需要循环.高亮最后一条.
      if (
        this.currentTime >= this.lyricArray[this.lyricArray.length - 2].time
      ) {
        return this.lyricArray.length - 2;
      }
      for (let i = 0; i < this.lyricArray.length; i++) {
        //获取当前高亮歌词的下标 计算属性依赖于currentTime lyricArray
        if (
          this.lyricArray[i].time <= this.currentTime &&
          this.currentTime < this.lyricArray[i + 1].time
        ) {
          return i;
        }
      }
      return 0;
    },
    lyricArray() {
      //处理歌词信息
      if (!this.lyric) return [];
      let larr = this.lyric.split("\n");
      let reg = /\[(\d+?):(\d+?\.\d+)\]([.\s\S]*)/;
      return larr.map((item) => {
        if (reg.test(item)) {
          return {
            time: parseInt(RegExp.$1) * 60 + parseFloat(RegExp.$2),
            lyric: RegExp.$3,
          };
        }
      });
    },
    processTime: {
      //v-model绑定计算属性。设置getter/setter
      get() {
        return (this.currentTime / this.totalTime) * 100;
      },
      set(value) {
        //当拖动修改range的值的时候，就修改播放时间.
        let ctime = (value / 100) * this.totalTime; //获取当前拖动的时间值.
        this.$emit("changetime", ctime); //反向传值修改当前播放音乐时间
      },
    },
  },
  watch: {
    music() {
      //当歌曲发生变化的时候，重新获取歌词.
      this.lyric = ""; //当音乐变化的时候得清空歌词.
      this.$emit("changetime", 0); //反向传值修改当前播放音乐时间
      this.getLyric();
    },
  },
  filters: {
    formatTime(value) {
      let minut = parseInt(value / 60);
      let sec = parseInt(value % 60); //秒数
      if (isNaN(minut)) return "00:00";
      minut = minut < 10 ? "0" + minut : minut; //分钟数小于10 加0
      sec = sec < 10 ? "0" + sec : sec;
      return minut + ":" + sec;
    },
  },
};
</script>

<style lang="less" scoped>
.fullscreen {
  overflow: hidden;
  .cover {
    position: relative;
    width: 30vh;
    height: 30vh;
    border-radius: 50%;
    overflow: hidden;
    margin: 0 auto;
    animation: round 5s linear infinite;
    img {
      display: block;
      width: 100%;
    }
  }
  .closeLately {
    text-align: center;
    position: absolute;
    bottom: 0px;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    height: 50px;
    line-height: 40px;
  }
  .title {
    text-align: center;
    margin: 12px 0 20px 0;
    font-size: 20px;
    font-weight: 700;
  }
  .songMessage{
    text-align: center;
    margin-top: 10px;
  }
  .songTitleName{
    color: #ffffff;
    font-size: 18px;
    margin-bottom: 5px;
  }
  .songTitleSinger{
    font-size: 14px;
    color: #ccc;
  }
  .latelySongItem {
    overflow: hidden;
    margin: 0 15px 15px 15px;
    padding-bottom: 5px;
    display: flex;
  }
  .latelySongListBox {
    overflow: auto;
    height: 390px;
  }
  .heng {
    color: gray;
    font-size: 12px;
    margin: 0 5px 0 1px;
    height: 25px;
    line-height: 17px;
  }
  .itemBox {
    display: flex;
    flex: 1;
  }
  .latelySongItemName {
    font-size: 16px;
    margin-right: 5px;
  }
  .latelySongItemSinger {
    height: 25px;
    line-height: 18px;
    font-size: 12px;
    color: gray;
  }
  .close {
    position: absolute;
    top: 15px;
    left: 15px;
    color: #ccc;
    font-size: 24px;
  }
  img {
    width: 20px;
  }
  .previousSong {
    display: inline-block;
    font-size: 30px;
  }
  .previousSong img {
    width: 35px;
  }
  .nextSong img {
    width: 35px;
  }
  .latelySong img {
    width: 25px;
  }
  .lately {
    border-radius: 10px;
    margin-left: 9px;
    position: absolute;
    bottom: -100%;
    width: 95%;
    height: 500px;
    overflow: auto;
    z-index: 100;
    background-color: #262727;
    color: white;
    transition: all 0.5s ease-in-out;
  }

  .nextSong {
    display: inline-block;
    font-size: 30px;
  }
  .playorpause {
    display: inline-block;
    font-size: 50px;
    margin: 0px 28px 0 32px;
  }
  .latelySong {
    position: absolute;
    font-size: 30px;
    right: 30px;
    top: 13px;
  }

  .lyric {
    height: 41vh;
    overflow: hidden;
    position: relative;

    ul {
      position: absolute;
      width: 100%;
      top: 0px;
      transition: transform 0.5s linear;
      li {
        color: #ffffff;
        text-align: center;
        height: 30px;
        line-height: 30px;
        white-space: nowrap;
        &.highlight {
          font-size: 20px;
          font-weight: bold;
          color: #e4393c;
        }
      }
    }
  }
  .controls {
    .range {
      display: flex;
      padding: 15px 20px 0 20px;
      input {
        flex: 1;
      }
    }
    .ctrls {
      position: relative;
      text-align: center;
      button {
        &.active {
          color: red;
          background: burlywood;
        }
      }
      .module {
        position: absolute;
        top: 11px;
        left: 27px;
      }
      .module img {
        width: 30px;
      }
    }
  }
}
@keyframes round {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>